package com.example.myapplication.vision.idcard.activity;

import android.graphics.SurfaceTexture;
import android.media.Image;
import android.media.ImageReader;
import android.os.Bundle;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.vision.idcard.utils.Camera2Proxy;


/**
 * Create By TreeRoot
 * Create Time: 2024/01/24 10:39
 * Class Name: VisionCameraFragment
 * Description:
 *
 * @author TreeRoot
 */
public class VisionCameraFragment extends Fragment implements View.OnClickListener {
    /**
     * 关闭按钮
     */
    private ImageView mCloseIv;
    /**
     * 切换摄像头按钮
     */
    private ImageView mSwitchCameraIv;
    /**
     * 拍照按钮
     */
    private ImageView mTakePictureIv;
    /**
     * 相机预览View
     */
    private Camera2View mCameraView;
    /**
     * 相机代理
     */
    private Camera2Proxy mCameraProxy;
    /**
     * 预览的YUV数据
     */
    private byte[] mYuvBytes;

    /**
     * 是否拍照
     */
    private final TextureView.SurfaceTextureListener mSurfaceTextureListener
            = new TextureView.SurfaceTextureListener() {

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
            mCameraProxy.openCamera();
            mCameraProxy.setPreviewSurface(texture);
            // 根据相机预览设置View大小，避免显示变形
            Size previewSize = mCameraProxy.getPreviewSize();
            mCameraView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
            return true;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture texture) {
        }

    };
    /**
     * 处理图片的回调
     */
    private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
            = reader -> {
        Image image = reader.acquireLatestImage();
        if (image == null) {
            return;
        }
        processImage(image);
        //处理完一定要close
        image.close();
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.vision_fragment_vision_camera, null);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        mCloseIv = rootView.findViewById(R.id.toolbar_close_iv);
        mSwitchCameraIv = rootView.findViewById(R.id.toolbar_switch_iv);
        mTakePictureIv = rootView.findViewById(R.id.take_picture_iv);
        mCameraView = rootView.findViewById(R.id.camera_view);
        mCameraProxy = mCameraView.getCameraProxy();

        mCloseIv.setOnClickListener(this);
        mSwitchCameraIv.setOnClickListener(this);
        mTakePictureIv.setOnClickListener(this);
        mCameraProxy.setImageAvailableListener(mOnImageAvailableListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mCameraView.isAvailable()) {
            mCameraProxy.openCamera();
        } else {
            mCameraView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mCameraProxy.releaseCamera();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.toolbar_close_iv) {
            getActivity().finish();
        }
        if (v.getId() == R.id.toolbar_switch_iv) {
            mCameraProxy.switchCamera();
        }
        if (v.getId() == R.id.take_picture_iv) {
            boolean mIsShutter = true;
        }
    }

    /**
     * 处理图片
     *
     * @param image 图片
     */
    private void processImage(Image image) {
//        int width = mCameraProxy.getPreviewSize().getWidth();
//        int height = mCameraProxy.getPreviewSize().getHeight();
//        if (mYuvBytes == null) {
//            // YUV420 大小总是 width * height * 3 / 2
//            mYuvBytes = new byte[width * height * 3 / 2];
//        }
//
//        ColorConvertUtil.getI420FromImage(image, mYuvBytes);
//
//        if (mIsShutter) {
//            mIsShutter = false;
//            // save yuv data
//            String yuvPath = FileUtil.SAVE_DIR + System.currentTimeMillis() + ".yuv";
//            FileUtil.saveBytes(mYuvBytes, yuvPath);
//            // save bitmap data
//            String jpgPath = yuvPath.replace(".yuv", ".jpg");
//            Bitmap bitmap = ColorConvertUtil.yuv420pToBitmap(mYuvBytes, width, height);
//            FileUtil.saveBitmap(bitmap, jpgPath);
//            bitmap.recycle();
//        }
    }
}